﻿using Microsoft.VisualStudio.Shell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using System.Windows;
using Microsoft.VisualStudio.TextManager.Interop;
using Rechy.RechyVSTool.Common;
using System.Threading;

namespace Rechy.RechyVSTool.Test
{
    class Window
    {
        public static string ListDocFrames(IVsUIShell uiShell)
        {
            IEnumWindowFrames winFrams;
            uiShell.GetDocumentWindowEnum(out winFrams);

            String s = String.Empty;

            IVsWindowFrame[] frames = new IVsWindowFrame[1];
            uint numFrames;

            while (winFrams.Next(1, frames, out numFrames) == VSConstants.S_OK && numFrames == 1)
            {
                object obj;
                WinFrame winframe = new WinFrame();

                frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_Caption, out obj);
                winframe.title = obj as String;

                frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_DocView, out obj);
                winframe.codeWindow = obj as IVsCodeWindow;
                if(null == winframe.codeWindow)
                {
                    frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out obj);
                    winframe.codeWindow = obj as IVsCodeWindow;
                }

                frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_UserContext, out obj);
                winframe.userContext = obj as IVsUserContext;

                frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_OwnerCaption, out obj);
                winframe.ownerName = obj as String;

                frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_ToolbarHost, out obj);
                winframe.toolbarHost = obj as IVsToolWindowToolbarHost;
                winframe.toolbarHost.Show(0);

                frames[0].GetGuidProperty((int)__VSFPROPID.VSFPROPID_CmdUIGuid, out winframe.cmdGuid);
                frames[0].GetGuidProperty((int)__VSFPROPID.VSFPROPID_CmdUIGuid, out winframe.autoActivateGuid);
                frames[0].GetGuidProperty((int)__VSFPROPID.VSFPROPID_CmdUIGuid, out winframe.persistenceSlotGuid);

                s += winframe.title + "\n";
                s += winframe.ownerName + "\n";
                s += winframe.cmdGuid.ToString() + "\n";
                s += winframe.autoActivateGuid.ToString() + "\n";
                s += winframe.persistenceSlotGuid.ToString() + "\n";
                s += "\n";
            }
            return s;
        }

        public static string ListToolFrames(IVsUIShell uiShell)
        {
            IEnumWindowFrames winFrams;
            uiShell.GetToolWindowEnum(out winFrams);

            String s = String.Empty;

            IVsWindowFrame[] frames = new IVsWindowFrame[1];
            uint numFrames;

            while (winFrams.Next(1, frames, out numFrames) == VSConstants.S_OK && numFrames == 1)
            {
                object obj;
                WinFrame winframe = new WinFrame();

                frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_Caption, out obj);
                winframe.title = obj as String;

                frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_DocView, out obj);
                winframe.codeWindow = obj as IVsCodeWindow;
                if (null == winframe.codeWindow)
                {
                    frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out obj);
                    winframe.codeWindow = obj as IVsCodeWindow;
                }

                frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_Type, out obj);
                winframe.type = null == obj ? 0 : int.Parse(obj.ToString());

                frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_UserContext, out obj);
                winframe.userContext = obj as IVsUserContext;

                frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_OwnerCaption, out obj);
                winframe.ownerName = obj as String;

                frames[0].GetProperty((int)__VSFPROPID.VSFPROPID_ToolbarHost, out obj);
                winframe.toolbarHost = obj as IVsToolWindowToolbarHost;
                winframe.toolbarHost.Show(0);

                frames[0].GetGuidProperty((int)__VSFPROPID.VSFPROPID_CmdUIGuid, out winframe.cmdGuid);
                frames[0].GetGuidProperty((int)__VSFPROPID.VSFPROPID_CmdUIGuid, out winframe.autoActivateGuid);
                frames[0].GetGuidProperty((int)__VSFPROPID.VSFPROPID_CmdUIGuid, out winframe.persistenceSlotGuid);

                s += "=" + winframe.title + "\n";
                s += "=" + winframe.type + "\n";
                s += "=" + winframe.toolbarHost.ToString() + "\n";
                s += "=" + winframe.ownerName + "\n";
                s += "=" + winframe.cmdGuid.ToString() + "\n";
                s += "=" + winframe.autoActivateGuid.ToString() + "\n";
                s += "=" + winframe.persistenceSlotGuid.ToString() + "\n";
                s += "=" + winframe.userContext.ToString() + "\n";
                s += "\n";
            }
            return s;
        }

        public static string ListProject()
        {
            //propId == (int)__VSHPROPID2.VSHPROPID_PropertyPagesCLSIDList
            return "";
        }
    }
}
